﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CNVP.Framework;
using CNVP.JShop.UI;
using CNVP.Framework.Utils;

namespace CNVP.JShop.Web.Admin
{
    public partial class PrintExpress : AdminPage
    {
        BLL.CNVP_Order bll = new BLL.CNVP_Order();
        Model.CNVP_Order model = new Model.CNVP_Order();
        public int id = 0;
        public string Province = "0", City = "0", Area = "0";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!BLL.BLLCheckAdmin.LoginOk(Page, 94)) return;//判断页面权限
            if (!string.IsNullOrEmpty(Request.QueryString["ID"]) && Public.IsNumber(Request.QueryString["ID"]))
            {
                id = Convert.ToInt32(Request.QueryString["ID"]);
            }
            if (!IsPostBack)
            {
                ProvinceBind();
                DataInfo();
            }
            ddlProvince.Attributes.Add("onchange", "ProvinceChange()");
        }
        private void DataInfo()
        {
            model = bll.GetSingle_CNVP_OrderById(id);
            if (model != null)
            {
                txtGetTrueName.Text = model.GetTrueName;
                txtGetAddress.Text = model.GetAddress;
                txtMobilePhone.Text = model.MobilePhone;
                txtPhone.Text = model.Phone;
                txtZipCode.Text = model.ZipCode;
                txtText.Text = model.Text;
                ddlProvince.SelectedValue = model.Province;
                Province = model.Province;
                City = model.City;
                Area = model.Area;
            }

            //发货地址
            ddlPostAddress.DataSource = new BLL.CNVP_PostAddress().GetAll_CNVP_PostAddress();
            ddlPostAddress.DataTextField = "PostTitle";
            ddlPostAddress.DataValueField = "ID";
            ddlPostAddress.DataBind();

            //配送方式
            string litText = "";
            IList<Model.CNVP_Column> model_DeliveryWay = new BLL.CNVP_Column().GetAll_CNVP_Column("ColumnType=4 Order by ID Desc");
            foreach (Model.CNVP_Column D in model_DeliveryWay)
            {
                litText += "<input type=\"button\" value=\"" + D.ColumnName + "\" onclick=\"RedirectToPrint(" + id + "," + D.ID + ")\" />";
            }
            literText.Text = litText;
            ddlPostAddress.Attributes.Add("onchange", "PostAddressChange()");

        }

        private void ProvinceBind()
        {
            IList<Model.CNVP_Province> model = new BLL.CNVP_Province().GetAll_CNVP_Province();
            foreach (Model.CNVP_Province p in model)
            {
                ddlProvince.Items.Add(new ListItem(p.Name, p.Code));
            }
        }

        protected void btSaveAddress_Click(object sender, EventArgs e)
        {
            try
            {
                model.ID = id;
                model.Province = ddlProvince.SelectedValue;
                model.City = Request.Form["ListCity"];
                model.Area = Request.Form["ListArea"];
                model.GetAddress = txtGetAddress.Text;
                model.GetTrueName = txtGetTrueName.Text;
                model.MobilePhone = txtMobilePhone.Text;
                model.Phone = txtPhone.Text;
                model.ZipCode = txtZipCode.Text;
                model.Text = txtText.Text;
                bll.Update_CNVP_OrderAddress(model);
                BasePage.MessageBox("修改成功！");
                DataInfo();
            }
            catch
            {
                BasePage.MessageBox("修改失败！");
            }

        }
    }
}